package com.org.khtn.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Quản lý các field
 * 
 * @author Son
 * 
 */
public class Fields {
	List<Field> fieldList;
	private final String className = "Fields:";
	Map<String, Map<String, String>> data;

	public Fields() {
		fieldList = new ArrayList<Field>();

	}

	/**
	 * 
	 * @return chuỗi string chứa các field có format : "name:address"
	 */
	public String getAllFieldName() {
		final String methodName = "getAllFieldName(): ";
		int length = fieldList.size();
		String result = "";
		if (length < 1)
			return result;
		result = fieldList.get(0).getFieldName();
		for (int i = 1; i < length; i++) {
			result = result.concat(":" + fieldList.get(i).getFieldName());
		}
		System.out.println(className + methodName + "result :" + result);

		return result;

	}

	/**
	 * 
	 * @return chuỗi string chứa các types có format : "int:text"
	 */

	public String getAllTypes() {
		final String methodName = "getAllTypes()";
		int length = fieldList.size();
		String result = "";
		if (length < 1)
			return result;
		result = fieldList.get(0).getFieldType();
		for (int i = 1; i < length; i++) {
			result = result.concat(":" + fieldList.get(i).getFieldType());
		}
		return result;

	}

	public void getData(Map<String, Map<String, String>> Data) {
		data = Data;
	}

	/**
	 * 
	 * @param fieldNames
	 *            có dạng như sau name:address:phone
	 * @param fieldTypes
	 *            có dạng như sau text:text:text
	 */
	public Fields(String fieldNames, String fieldTypes) {
		String[] temp1 = fieldNames.split(":");
		String[] temp2 = fieldTypes.split(":");

		int length = temp1.length;
		fieldList = new ArrayList<Field>();

		for (int i = 0; i < length; i++) {
			fieldList.add(new Field(temp1[i], temp2[i]));
		}

	}

	public void add(Field field) {
		int length = fieldList.size();
		for (Field f : fieldList)
			if (f.getObject(field.getFieldName()) != null) {
				f.setFieldType(field.getFieldType());
				return;
			}
		fieldList.add(field);

	}

	public void addAll(Collection col) {
		fieldList.addAll(col);
	}

	public Boolean exist(String name) {
		int length = fieldList.size();
		for (int i = 0; i < length; i++) {
			Field temp = (Field) fieldList.get(i);
			if (temp.getFieldName().equalsIgnoreCase(name))
				return true;
		}
		return false;

	}

	public int size() {
		return fieldList.size();
	}

	public void edit(String fieldName, String fieldType) {
		// TODO Auto-generated method stub
		add(new Field(fieldName, fieldType));
	}
	/**
	 * @param fieldName
	 */

	public void remove(String fieldName) {
		// TODO Auto-generated method stub
		for (Field f : fieldList)
			if (f.getFieldName().equalsIgnoreCase(fieldName)) {
				fieldList.remove(f);

				return;
			}

	}

	public void rename(String fieldName, String fieldNameNew) {
		// TODO Auto-generated method stub
		for (Field f : fieldList)
			if (f.getFieldName().equalsIgnoreCase(fieldName)) {
				Field temp = new Field(f.getFieldName(), f.getFieldType());
				temp.setFieldName(fieldNameNew);
				fieldList.remove(f);
				fieldList.add(temp);
				return;
			}

	}

}
